// pages/hall/hall.js
var interstitialAd = null;
Page({
  data: {
    searchText: "",
    BgColor: "rgb(168, 168, 168)", //按钮初始背景
    buttonLeft: "8rpx", //按钮小圆圈的位置
    loseLength: [],
    seekLength: [],
    loselength: 0,
    seeklength: 0,
    nowThing: '',
    seek: [],
    lose: [],
    page1: 1,
    page2: 1,
    indexTitle: 0,
  },
  //标题的切换
  /*标题的切换 */
  indexTitle1: async function () {
    this.setData({
      indexTitle: 0,
      seek: [],
      lose: [],
      seeklength: 0,
      loselength: 0,
      page2: 1,
      page1: 1
    }), await this.getnum1(),await this.getnum2(), await this.getGoodsList()
  },
  indexTitle2: async function () {
    this.setData({
      indexTitle: 1,
      seek: [],
      lose: [],
      seeklength: 0,
      loselength: 0,
      page2: 1,
      page1: 1
    }), 
    await this.getnum1(),await this.getnum2(), await this.getGoodsList()
  },
  onLoad: async function (e) {
    this.setData({
      searchText: e.id
    })
    await this.getnum1(), await this.getnum2(), await this.getGoodsList()
  },
  getnum1: async function () {
    const db = wx.cloud.database();
    var count = await db.collection('loseThing').where({
      title: db.RegExp({
        regexp: '.*' + this.data.searchText + '.*',
        options:'i'
      })
    }).count();
    var count = count.total;
    var page1=Math.ceil(count/10)
    this.setData({
      loselength: count,
      page1: Math.ceil(count / 10),
      yushu1:count%10
    })
  },
  getnum2: async function () {
    const db = wx.cloud.database();
    var count = await db.collection('seekThing').where({
      seekTitle: db.RegExp({
        regexp: '.*' + this.data.searchText + '.*',
        options:'i'
      })
    }).count();
    var count = count.total;
    var page2=Math.ceil(count/10)
    this.setData({
      seeklength: count,
      page2: Math.ceil(count / 10),
      yushu2:count%10
    })
  },

  getGoodsList: function () {
    const db = wx.cloud.database();
    if(0==this.data.indexTitle){//失物招领
      db.collection('loseThing').where({
        title: db.RegExp({
          regexp: '.*' + this.data.searchText + '.*',
          options:'i'
        })
      })
      .skip( this.data.page1==1?0:((this.data.page1-2)*10+this.data.yushu1) ).limit(this.data.page1==1?this.data.yushu1:10)
      .get().then(res => {
        for (var s=[],i=9;i>=0;i--) {
          s.push(res.data[i])
        }
        this.setData({
          lose:this.data.lose.concat(s)
        })
      }).catch(err => {
        console.log(err)
      })
    }
    // 寻物启事
    if(1==this.data.indexTitle){
      db.collection('seekThing').where({
        seekTitle: db.RegExp({
          regexp: '.*' + this.data.searchText + '.*',
          options:'i'
        })
      })
      .skip( this.data.page2==1?0:((this.data.page2-2)*10+this.data.yushu2) ).limit(this.data.page2==1?this.data.yushu2:10)
      .get().then(res => {
        for (var s=[],i=9;i>=0;i--) {
          s.push(res.data[i])
        }
        this.setData({
          seek:this.data.seek.concat(s)
        })
      }).catch(err => {
        console.log(err)
      })
    }
  },
  // onShareAppMessage(){
  //   return{
  //     title:'这是你的物品吗?',
  //     imageUrl:this.data.lose.bigImg
  //   }
  // },
  
  loseContent: function (t) {
    if (0 == this.data.indexTitle) {
      var e = this.data.lose[t.currentTarget.dataset.index]._id;
      wx.navigateTo({
        url: "../../hall/loseContent/loseContent?id=" + e
      });
    } else {
      e = this.data.seek[t.currentTarget.dataset.index]._id;
      wx.navigateTo({
        url: "../../hall/seekContent/seekContent?id=" + e
      });
    }
  },
  onReachBottom:async function () {
    if (0 == this.data.indexTitle) {
      if (1 == this.data.isLoading) return;
      if (this.data.lose.length >= this.data.loselength) return void wx.showToast({
        title: "数据加载完毕",
        icon: "none"
      });
      var page1=page1-1
      this.setData({
        page1: this.data.page1 - 1
      }), this.getGoodsList();
    } else {
      if (1 == this.data.isLoading) return;
      if (this.data.seek.length >= this.data.seeklength) return void wx.showToast({
        title: "数据加载完毕",
        icon: "none"
      });
      var page2=page2-1
      this.setData({
        page2: this.data.page2 - 1
      }), this.getGoodsList();
    }
  },
  onPullDownRefresh: async function () {
    this.setData({
      indexTitle: 0,
      seek: [],
      lose: [],
      seeklength: 0,
      loselength: 0,
      page2: 1,
      page1: 1
    }), await this.getnum1(),await this.getnum2(), await this.getGoodsList()
  },

})